<?php
session_cache_expire(999999999);
if (!isset($_SESSION)) {
    session_start();
}
$lang = $_SESSION['lang'];
if (empty($lang) || $lang == "EN") {
    include "../../value/value_en.php";
} else if ($lang == "VN") {
    include "../../value/value_vn.php";
} else {
    include "../../value/value_kr.php";
}
include '../../operations/connection.php';
include '../../content-accountant/invoicetax.php';
$stonggiacungcap = 0;
$stongthue = 0;
$stongtien = 0;
$arr = array();
$mangqui = array("('1','2','3')", "('4','5','6')", "('7','8','9')", "('10','11','12')");
$status = $_REQUEST['status'];
if ($status == 0) {
    $l_ngaygiaodich = $l_ngaygiaodich0;
    $l_ngayphathanh = $l_ngayphathanh0;
    $l_thoigiangiaodich = $l_thoigiangiaodich0;
    $l_thoigianphathanh = $l_thoigianphathanh0;
    $type = $l_phathanh;
}
if ($status == 1) {
    $l_ngaygiaodich = $l_ngaygiaodich1;
    $l_ngayphathanh = $l_ngayphathanh1;
    $l_thoigiangiaodich = $l_thoigiangiaodich1;
    $l_thoigianphathanh = $l_thoigianphathanh1;
    $type = $l_giaodich;
}
$typedate = $_REQUEST['typedate'];
$checkshow = $_REQUEST['checkshow'];
if ($typedate == 'ngayph') {
    $time = 'order_time';
    //$type=$l_phathanh;
    $thoigianphgd = $l_thoigianphathanh;
}
if ($typedate == 'ngaygd') {
    $time = 'order_time_transaction';
    //$type=$l_giaodich;
    $thoigianphgd = $l_thoigiangiaodich;
}
if (isset($_REQUEST['loaitimkiem'])) {
    if ($_REQUEST['loaitimkiem'] == "qui") {
        $qui = $mangqui[$_REQUEST['qui'] - 1];
        $sql = "SELECT or_order_tax.*   
		from or_order_tax  
		where or_order_tax.order_cusname like '%" . $_REQUEST['cusname'] . "%'";
        $sql .=" and ( month(or_order_tax.$time) in " . $qui . " and year(or_order_tax.$time) = '" . $_REQUEST['nam'] . "' ) ";
        $sql.="and is_status=" . $status . " order by or_order_tax.$time";
        if ($time == "order_time") {
            $sql.=",or_order_tax.order_time_transaction,or_order_tax.order_cusname,or_order_tax.order_content;";
        }
        if ($time == "order_time_transaction") {
            $sql.=",or_order_tax.order_time,or_order_tax.order_cusname,or_order_tax.order_content;";
        }
        $_SESSION['sqlinvoice1'] = $sql;
        $sqlinvoice = mysql_query($sql);
        $dem = 0;
        if (mysql_num_rows($sqlinvoice) > 0) {
            while ($rin = mysql_fetch_array($sqlinvoice)) {
                $dem++;
                $ob = new invoicetax();
                if (isset($_REQUEST['not'])) {
                    if ($_REQUEST['not'] == 'false') {
                        if ($rin[$time] == '0000-00-00' || $rin[$time] == '')
                            $ktdate = 0;
                        else
                            $ktdate=getdate(strtotime($rin[$time]));

                        $ob->Instance($rin['orderid'], $rin['order_time'], $ktdate['mon'], $ktdate['day'], $ktdate['year'], $rin['order_cus'], $rin['order_content'], $rin['order_quantity'], $rin['order_supply_rice'], $rin['order_tax'], $rin['order_total'], $rin['order_time_transaction'], $rin['is_status'], $rin['user_create'], $rin['date_create'], $rin['order_memo'], $rin['status']);
                        $ob->setCusname($rin['order_cusname']);
                        $ob->setPayment($rin['order_time_payment']);
                        $ob->setRED($rin['red']);
                        if (array_key_exists($ktdate['year'] . '-' . $ktdate['mon'], $arr) == true) {
                            array_push($arr[$ktdate['year'] . '-' . $ktdate['mon']], $ob);
                        } else {
                            $arr[$ktdate['year'] . '-' . $ktdate['mon']] = array();
                            $arr[$ktdate['year'] . '-' . $ktdate['mon']][] = $ob;
                        }
                    }
                    if ($_REQUEST['not'] == 'true') {
                        if ($rin['order_time_transaction'] == '0000-00-00' || $rin['order_time_transaction'] == '') {
                            if ($rin[$time] == '0000-00-00' || $rin[$time] == '')
                                $ktdate = 0;
                            else
                                $ktdate=getdate(strtotime($rin[$time]));

                            $ob->Instance($rin['orderid'], $rin['order_time'], $ktdate['mon'], $ktdate['day'], $ktdate['year'], $rin['order_cus'], $rin['order_content'], $rin['order_quantity'], $rin['order_supply_rice'], $rin['order_tax'], $rin['order_total'], $rin['order_time_transaction'], $rin['is_status'], $rin['user_create'], $rin['date_create'], $rin['order_memo'], $rin['status']);
                            $ob->setCusname($rin['order_cusname']);
                            $ob->setPayment($rin['order_time_payment']);
                            $ob->setRED($rin['red']);
                            if (array_key_exists($ktdate['year'] . '-' . $ktdate['mon'], $arr) == true) {
                                array_push($arr[$ktdate['year'] . '-' . $ktdate['mon']], $ob);
                            } else {
                                $arr[$ktdate['year'] . '-' . $ktdate['mon']] = array();
                                $arr[$ktdate['year'] . '-' . $ktdate['mon']][] = $ob;
                            }
                        }
                    }
                } else {
                    if ($rin[$time] == '0000-00-00' || $rin[$time] == '')
                        $ktdate = 0;
                    else
                        $ktdate=getdate(strtotime($rin[$time]));

                    $ob->Instance($rin['orderid'], $rin['order_time'], $ktdate['mon'], $ktdate['day'], $ktdate['year'], $rin['order_cus'], $rin['order_content'], $rin['order_quantity'], $rin['order_supply_rice'], $rin['order_tax'], $rin['order_total'], $rin['order_time_transaction'], $rin['is_status'], $rin['user_create'], $rin['date_create'], $rin['order_memo'], $rin['status']);
                    $ob->setCusname($rin['order_cusname']);
                    $ob->setPayment($rin['order_time_payment']);
                    $ob->setRED($rin['red']);
                    if (array_key_exists($ktdate['year'] . '-' . $ktdate['mon'], $arr) == true) {
                        array_push($arr[$ktdate['year'] . '-' . $ktdate['mon']], $ob);
                    } else {
                        $arr[$ktdate['year'] . '-' . $ktdate['mon']] = array();
                        $arr[$ktdate['year'] . '-' . $ktdate['mon']][] = $ob;
                    }
                }
            }
            $_SESSION['manginvoicetax'] = $arr;
        }
    }
}
$addressbrowser = $_SERVER['SERVER_NAME'];
$temp = explode(".", $addressbrowser);
$queryDepartment = mysql_query("SELECT d.IDDepartment,dl.DepartmentName FROM department d,department_lang dl WHERE d.IDDepartment=dl.IDDepartment AND dl.LangID='$lang';");
$querySendMail = mysql_query("SELECT e.IDEmployee,e.FullName FROM list_sendmail l,employee e WHERE l.employee_id=" . $_SESSION['IDEmployee'] . " AND e.IDEmployee=l.employee_id_sendmail GROUP BY l.employee_id_sendmail");
include '../../popup/popup_reference1.php';
?>
<div class="title2">
        <!-- <span style="float:left"><?php echo $l_company_name ?> : ELisoft</span>
        <span style="float:right">
<?php
if ($_REQUEST['cusname'] != "") {
    ?>
        <?php echo $l_tenkhachhanggiaodich; ?> : <?php echo $_REQUEST['cusname']; ?>
        <?php
    }
    ?>
        
			        </span> -->
    <input type="hidden" id="orderid" value=""/>
    <input type="hidden" id="lang" value=""/>
    <input type="hidden" id="company" value="" />
</div>
<div style="clear:both"></div>
<div style="position:relative" id="listEmp" >
    <div>
        <table  width="100%" border="0" class="ftable" >
            <tr>
                <th style="width: 2%;min-width: 40px"><a onclick="delorder('<?= $_REQUEST['loaitimkiem'] ?>',<?= $status ?>,'<?= $typedate ?>','<?= $_REQUEST['not'] ?>',<?= $dem ?>)"><img src="images/del.png" title="<?php echo $l_delete; ?>"/></a></th>
                <th style="width: 2%;min-width: 40px" ><label><input type="checkbox" checked="checked" id="check_no"><?= $l_no ?></label></th>
                <th style="width: 10%;min-width: 100px"><label><input type="checkbox" checked="checked" id="check_ngaythangnam"><?= $l_ngaythangnam ?></label></th>
                <th style="width: 18%;min-width: 170px"><label><input type="checkbox" checked="checked" id="check_company_name"><?= $l_company_name ?></label></th>
                <th style="width: 35%;min-width: 220px"><label><input type="checkbox" checked="checked" id="check_product_item"><?= $l_product_item ?></label></th>
                <th style="display: none;"><label><input type="checkbox" id="check_quantity"><?= $l_quantity ?></label></th>
                <th style="width: 8%;min-width: 80px"><label><input type="checkbox" checked="checked" id="check_Supply_price"><?= $l_Supply_price ?></label></th>
                <th style="width: 8%;min-width: 80px"><label> <input type="checkbox" checked="checked" id="check_vat"><?= $l_vat ?></label></th>
                <th style="width: 8%;min-width: 80px"><label><input type="checkbox" checked="checked" id="check_amount"><?= $l_amount ?></label></th>
                <th style="width: 10%;min-width: 100px"><label><input type="checkbox" id="check_thoigiangiaodich"><?= $l_thoigiangiaodich ?></label></th>
                <th style="width: 10%;min-width: 100px;<?php if($status==0) echo 'display: none'?>" ><?= $l_payment ?></th>
                <th style="width: 10%;min-width: 120px"><label><input type="checkbox" id="check_memo"><?= $l_memo ?></label></th>
                <th style="width: 8%;min-width: 80px"></th>
            </tr>

<?php
$soluong = count($arr);
if ($soluong > 0) {
    $vitri = 0;
    foreach ($arr as $thoigian => $mgiatri) {
        $tonggiacungcap = 0;
        $tongthue = 0;
        $tongtien = 0;
        $dem = count($mgiatri);
        $d = 0;
        if ($dem > 0) {
            for ($i = 0; $i < $dem; $i++) {
                $inobject = unserialize(serialize($mgiatri[$i]));
                ?>
                            <tr style="display: <?=$checkshow?>;<?php if($status==1){if($inobject->red==1) echo 'background: #FF9999;';if($inobject->red==0) echo 'background: #d0d8e8;';}?>">
                                <td><input type="checkbox" id="check<?= $vitri ?>" name="check<?= $vitri ?>" value="<?= $inobject->orderid ?>" /></td>
                                <td style="padding: 5px;"><?= ($vitri + 1) ?></td>
                                <td><?= $inobject->ordertime ?></td>
                                <td style="text-align:left"><?= $inobject->cusname ?></td>
                                <td style="text-align:left"><?= $inobject->order_content ?></td>
                                <td style="display: none;"><?= number_format($inobject->order_quantity) ?></td>
                                <td style="text-align:right;"><?= number_format($inobject->order_suplly_rice) ?></td>
                                <td style="text-align:right;"><?= number_format($inobject->order_tax) ?></td>
                                <td style="text-align:right;"><?= number_format($inobject->order_total) ?></td>
                                <td><?php
                if ($inobject->nstatus == 1)
                    echo "-";
                else
                    echo $inobject->order_time_transaction;
                ?></td>
                				<?php if($status==1){?><td><?php  echo $inobject->order_time_payment; ?></td><?php }?>
                                <td><?= $inobject->order_memo ?></td>
                                <td><img src="images/email.png" onclick="setdata('<?= $inobject->orderid ?>','<?= $lang ?>','<?= $temp[0] ?>')" title="<?= $l_sendmail ?>" /></td>
                            </tr>
                <?php
                if ($inobject->order_time_transaction != "") {
                    $d++;
                }
                $tonggiacungcap +=$inobject->order_suplly_rice;
                $tongthue +=$inobject->order_tax;
                $tongtien +=$inobject->order_total;
                $vitri++;
            }
        }
        ?>
                    <tr class="coltr2" style="font-weight:bold;font-size:14px;">
                        <td colspan="5"  style="padding: 10px;">
                    <?php
                    if ($_REQUEST['loaitimkiem'] == "thang") {
                        ?>
                                <?php echo $l_month; ?> : <font color="red"><?php echo $thoigian; ?></font>  -  <?php echo $type; ?> : <font color="blue"><?php echo number_format($d); ?></font> 
                                <?php
                            } else if ($_REQUEST['loaitimkiem'] == "qui") {
                                ?>
                                <?php echo $l_month; ?> : <font color="red"><?php echo $thoigian; ?></font>  -  <?php echo $type; ?> : <font color="blue"><?php echo number_format($d); ?></font> 
                                <?php
                            } else {
                                ?>
                                <?php echo $l_days; ?> : <font color="red"><?php echo $thoigian; ?></font>  -  <?php echo $type; ?> : <font color="blue"><?php echo number_format($d); ?></font> 
                                <?php
                            }
                            ?>
                        </td>
                        <td style="display: none;"></td>
                        <td style="text-align:right;"><?php echo number_format($tonggiacungcap); ?></td>
                        <td style="text-align:right;"><?php echo number_format($tongthue); ?></td>
                        <td style="text-align:right;"><?php echo number_format($tongtien); ?></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <?php if($status==1){?><td></td><?php }?>
                    </tr>
        <?php
        $stonggiacungcap +=$tonggiacungcap;
        $stongthue +=$tongthue;
        $stongtien +=$tongtien;
    }
} else {
    ?>
                <tr><td colspan="12" style="height:30px;text-align:left;padding-left:20px;"><?php echo $l_list_emplty; ?></td></tr>
                <?php
            }
            ?>

            <tr style="font-weight:bold;font-size:15px;color:green;">
                <td colspan="5" style="padding: 10px;"><?php echo $l_tongdon ?></td>
                <td style="display: none;"></td>
                <td style="text-align:right;"><?php echo number_format($stonggiacungcap); ?></td>
                <td style="text-align:right;"><?php echo number_format($stongthue); ?></td>
                <td style="text-align:right;"><?php echo number_format($stongtien); ?></td>
                <td style="text-align:right;"></td>
                <td></td>
                <td></td>
                <?php if($status==1){?><td></td><?php }?>
            </tr>
        </table>
    </div>
</div>
<?php
if ($soluong > 0) {
    if ($_REQUEST['loaitimkiem'] == "qui") {
        ?>
        <div class="fbox2" style="text-align:center; width:100%;">
            <a href="printer/ajax_export_acc.php?type=qui&typedate=<?php echo $typedate; ?>&cusname=<?php echo $_REQUEST['cusname']; ?>&df=<?php echo $_REQUEST['df']; ?>&dt=<?php echo $_REQUEST['dt']; ?>&status=<?= $status ?>"><input type="button" value="<?= $l_export_excel ?>" class="btnupbg btnupload" style="background: #4f81bd;"/></a>
            <a onclick="onprinter('<?php echo $_REQUEST['loaitimkiem']; ?>','<?php echo $typedate; ?>','<?php echo $_REQUEST['cusname']; ?>','<?php echo $_REQUEST['nam']; ?>','<?php echo $_REQUEST['qui']; ?>','<?= $status ?>');"><input type="button" value="<?= $l_print ?>" class="btnupbg btnupload" style="background: #4f81bd;"/></a>
        </div>
        <?php
    }
}
?>
        		
